package com.pubinfo.passbook.data.controller.sync;

import com.alibaba.fastjson.JSONObject;
import com.pubinfo.passbook.common.entity.TPassbook;
import com.pubinfo.passbook.common.mapper.TSourcePassbookMapper;
import com.pubinfo.passbook.common.model.bo.passbook.medical.MedicalEInvoice;
import com.pubinfo.passbook.common.model.dto.financial.CancelClaimFeed;
import com.pubinfo.passbook.common.model.dto.financial.CancelLockedRecord;
import com.pubinfo.passbook.common.model.dto.financial.FeedbackParam;
import com.pubinfo.passbook.common.model.dto.financial.InvoiceResult;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.master.TPassbookService;
import com.pubinfo.passbook.common.service.request.financial.RequestFinancialServer;
import com.pubinfo.passbook.common.utils.XmlTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "debug/financial")
@Api(tags = "同步财政数据管理")
@Slf4j
public class SyncFinancialController {
  @Autowired
  RequestFinancialServer financialServer;
  @Autowired
  TSourcePassbookMapper passbookMapper;
  @Autowired
  TPassbookService passbookService;
  
  //
//  @PostMapping("syncByDate")
//  @ApiOperation(value = "指定时间内进行同步操作 时间格式 yyyyMMdd")
//  public JsonResult<Integer> listByName(String startDate, String endDate) {
//    return JsonResult.success(financialServer.sync(startDate, endDate));
//  }
  @PostMapping("syncAllByDate")
  @ApiOperation(value = "指定时间内进行同步操作 时间格式 yyyyMMdd")
  public JsonResult<Integer> syncAllByDate(String startDate, String endDate) {
    return JsonResult.success(financialServer.syncAll(startDate, endDate));
  }
  
  @PostMapping("selectByDate")
  @ApiOperation(value = "指定时间内查询某个公司的票据列表 7天内 时间格式 yyyyMMdd")
  public List<TPassbook> syncAllByDate(String startDate, String endDate, String nsrsbh) {
    return financialServer.subsidiaryBillList(startDate, endDate, nsrsbh);
  }
  
  @GetMapping("selectRepetition")
  @ApiOperation(value = "查询重复数据")
  public JsonResult<List<TPassbook>> selectRepetition() {
    return JsonResult.success(passbookMapper.selectRepetition());
  }
  
  @PostMapping("claimFeedback")
  @ApiOperation(value = "回写票据")
  public JsonResult<Boolean> claimFeedback(@RequestBody FeedbackParam feedbackParam) {
    return JsonResult.success(financialServer.claimFeedback(feedbackParam));
  }
  
  @PostMapping("cancelClaimFeed")
  @ApiOperation(value = "入账撤消")
  public JsonResult<Boolean> cancelClaimFeed(@RequestBody CancelClaimFeed claimFeed) {
    return JsonResult.success(financialServer.cancelClaimFeed(claimFeed));
  }
  
  @PostMapping("cancelLockedList")
  @ApiOperation(value = "批量解锁票据")
  public InvoiceResult<Object> cancelLockedList(@RequestBody String[] eInvoiceIds) {
    List<CancelLockedRecord> cancelLockedRecords = CancelLockedRecord.toList(eInvoiceIds);
    InvoiceResult<Object> invoiceResult = financialServer.cancelLockedList(cancelLockedRecords, "取消");
    return invoiceResult;
  }
  
  @PostMapping("lockedByEinvoiceId")
  @ApiOperation(value = "根据id锁定票据")
  public InvoiceResult<Object> lockedByEinvoiceId(String[] eInvoiceIds) {
    List<CancelLockedRecord> cancelLockedRecords = CancelLockedRecord.toList(eInvoiceIds);
    InvoiceResult<Object> invoiceResult = financialServer.cancelLockedList(cancelLockedRecords, "取消");
    return invoiceResult;
  }
  
  @GetMapping("updateInvoice")
  @ApiOperation(value = "修改票据状态")
  public Boolean updateInvoice() {
    
    return passbookService.updateInvoice();
  }
  
  @PostMapping("toJson")
  @ApiOperation(value = "toJson")
  public JSONObject toJson(String xml) {
    
    return XmlTool.xml2json(xml);
  }
  
  @SneakyThrows
  @PostMapping("xml2java")
  @ApiOperation(value = "xml2java")
  public Object xml2java(String xml) {
    // String json= XmlTool.xml2jsonByJackson(xml);
    
    //  return JSON.parseObject(json,MedicalObject.class);
    //  JSONObject jsonObject =
    return XmlTool.convertToJavaBean(xml, MedicalEInvoice.class);
  }
}
